package com.ruoyi.system.domain;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;

public class Test01 {


	public static ExecutorService executorService = Executors.newFixedThreadPool(10);
	//红色开奖号码
	public static final String R_NEXT = "3 7 9 17 23 27";
	//蓝色开奖号码
	public static final String B_NEXT = "3";

	public static Map<Integer,Integer> map = new HashMap<>();

	public static void main(String[] args) {
		for (int j = 0; j < 10; j++) {
			CompletableFuture.supplyAsync(() -> task(), executorService);
		}
	}

	private static int task(){
		int i = 0;
		int c = 0;
		while (i != 1) {
			JSONObject json = generalDoubleColorBall();
			i = checkOne(json, R_NEXT, B_NEXT);
			if(i > 0){
				map.put(i,map.containsKey(i)?map.get(i)+1:1);
			}
			c++;
		}
		System.out.println("完成任务,执行次数:"+c*10);
		System.out.println("统计结果:");
		map.keySet().stream().forEach(
				t -> System.out.println(t+"等奖"+map.get(t)+"次")
		);
		System.exit(1);
		return c;
	}

	private static String check(JSONObject json, String r_next, String b_next) {
		json.getJSONArray("result").stream().forEach(
				o -> checkOne((JSONObject)o,r_next,b_next)
		);
		return null;
	}

	private static int checkOne(JSONObject c, String r_next, String b_next){
		Integer[] r = (Integer[])c.get("r-data");
		Object o_b = c.get("b-data");
		Integer[] b;
		if(o_b instanceof Integer){
			b = new Integer[1];
			b[0] = (Integer)o_b;
		}else{
			b = (Integer[])o_b;
		}
		int r1 = checkNumber(r,r_next);
		int r2 = checkNumber(b,b_next);
		int j = checkR(r1,r2);
		if(j<3 && j>0){
			//System.out.println(Thread.currentThread().getName()+"=>中"+j+"等奖啦!中奖号码:"+c.getString("des"));
		}
		return j;
	}

	private static int checkR(int r1,int r2){
		String r = r1+","+r2;
		switch (r){
			case "0,1": case "1,1": case "2,1":return 6;
			case "3,1": case "4,0": return 5;
			case "4,1": case "5,0": return 4;
			case "5,1": return 3;
			case "6,0": return 2;
			case "6,1": return 1;
			default:return 0;
		}
	}

	private static int checkNumber(Integer[] r, String r_next) {
		List<Integer> integers = new ArrayList<>(Arrays.asList(r));
		List<Integer> list = new ArrayList<>(Arrays.asList(r_next.split(" ")).stream()
				.mapToInt(Integer::parseInt).boxed().collect(Collectors.toList()));
		return list.retainAll(integers)?list.size():integers.size();
	}

	private static JSONObject generalDoubleColorBall(int n){
		StringBuilder s = new StringBuilder();
		JSONArray array = new JSONArray();
		while (n>0){
			JSONObject r = generalDoubleColorBall();
			s.append(r.getString("des"));
			array.add(r);
			n--;
		}
		JSONObject r = new JSONObject();
		r.put("des",s.toString());
		r.put("result",array);
		return r;
	}

	private static JSONObject generalDoubleColorBall(){
		Integer[] reds;
		Integer blue;
		reds = generalNumber(6,1,33);
		blue = generalNumber(1,1,16)[0];
		StringBuilder s = new StringBuilder();
		s.append(Arrays.toString(reds)
				.replace("[","")
				.replace("]","")
				.replace(",","\t")
		).append("+").append(blue).append("\n");
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("des",s.toString());
		jsonObject.put("r-data",reds);
		jsonObject.put("b-data",blue);
		return jsonObject;
	}

	private static Integer[] generalNumber(int i, int s, int e) {
		return new Random().ints(s, e+1).distinct().limit(i).boxed().sorted().toArray(Integer[]::new);
	}
}
